<template>
  <view class="heritage-register-container">
    <view class=" align-center justify-center flex">
      <image mode="widthFix" src="@/static/img/home.png"></image>
    </view>
    
    <view class="register-form-content">
      <view class="form-wrapper">
        <view class="input-item flex align-center">
          <view class="iconfont icon-user icon"></view>
          <input v-model="registerForm.username" class="input" type="text" placeholder="请输入账号" maxlength="30" />
        </view>
        <view class="input-item flex align-center">
          <view class="iconfont icon-password icon"></view>
          <input v-model="registerForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20" />
        </view>
        <view class="input-item flex align-center">
          <view class="iconfont icon-password icon"></view>
          <input v-model="registerForm.confirmPassword" type="password" class="input" placeholder="请再次输入密码" maxlength="20" />
        </view>
        <view class="input-item flex align-center" style="width: 100%;" v-if="captchaEnabled">
          <view class="iconfont icon-code icon"></view>
          <input v-model="registerForm.code" type="number" class="input" placeholder="请输入验证码" maxlength="4" />
          <view class="login-code">
            <image :src="codeUrl" @click="getCode" class="login-code-img"></image>
          </view>
        </view>
        
        <view class="action-btn">
          <button @click="handleRegister" class="register-btn">注 册</button>
        </view>
      </view>
    </view>
    
    <view class="bottom-links">
      <text @click="handleUserLogin" class="login-link">使用已有账号登录</text>
    </view>
  </view>
</template>

<script>
  import { getCodeImg, register } from '@/api/login'

  export default {
    data() {
      return {
        codeUrl: "",
        captchaEnabled: true,
        globalConfig: getApp().globalData.config,
        registerForm: {
          username: "",
          password: "",
          confirmPassword: "",
          code: "",
          uuid: ''
        }
      }
    },
    created() {
      this.getCode()
    },
    methods: {
      // 用户登录
      handleUserLogin() {
        this.$tab.navigateTo(`/pages/login`)
      },
      // 获取图形验证码
      getCode() {
        getCodeImg().then(res => {
          this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
          if (this.captchaEnabled) {
            this.codeUrl = 'data:image/gif;base64,' + res.img
            this.registerForm.uuid = res.uuid
          }
        })
      },
      // 注册方法
      async handleRegister() {
        if (this.registerForm.username === "") {
          this.$modal.msgError("请输入您的账号")
        } else if (this.registerForm.password === "") {
          this.$modal.msgError("请输入您的密码")
        } else if (this.registerForm.confirmPassword === "") {
          this.$modal.msgError("请再次输入您的密码")
        } else if (this.registerForm.password !== this.registerForm.confirmPassword) {
          this.$modal.msgError("两次输入的密码不一致")
        } else if (this.registerForm.code === "" && this.captchaEnabled) {
          this.$modal.msgError("请输入验证码")
        } else {
          this.$modal.loading("注册中，请耐心等待...")
          this.register()
        }
      },
      // 用户注册
      async register() {
        register(this.registerForm).then(res => {
          this.$modal.closeLoading()
          uni.showModal({
            title: "系统提示",
            content: "恭喜你，您的账号 " + this.registerForm.username + " 注册成功！",
            success: function (res) {
              if (res.confirm) {
                uni.redirectTo({ url: `/pages/login` });
              }
            }
          })
        }).catch(() => {
          if (this.captchaEnabled) {
            this.getCode()
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  page {
	background: linear-gradient(to bottom, #7C191E, #5e1317);
  }

  .heritage-register-container {
    width: 100%;
    min-height: 100vh;
    position: relative;
    display: flex;
    flex-direction: column;
    padding-top: 15%;
    
    .logo-content {
      margin-bottom: 30px;
      
      image {
        width: 80px;
        height: 80px;
        border-radius: 50%;
        border: 3px solid rgba(255, 255, 255, 0.2);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
        background-color:#7C191E;
        padding: 5px;
      }
    }
    
    .register-form-content {
      width: 85%;
      margin: 0 auto;
      flex: 1;
      
      .form-wrapper {
        background-color: rgba(255, 255, 255, 0.9);
        border-radius: 20px;
        padding: 30px 25px;
        box-shadow: 0 8px 20px rgba(58, 40, 24, 0.15);
        border: 1px solid #e8d9c5;
      }
      
      .input-item {
        margin: 20px 0;
        background-color: #fff;
        height: 50px;
        border-radius: 25px;
        border: 1px solid #e8d9c5;
        padding: 0 15px;
        
        .icon {
          font-size: 20px;
          color: #7C191E;
          margin-right: 10px;
        }
        
        .input {
          flex: 1;
          font-size: 15px;
          height: 100%;
          color: #3a2818;
          
          &::placeholder {
            color: #9e8b72;
          }
        }
      }
      
      .login-code {
        margin-left: 10px;
        
        .login-code-img {
          width: 100px;
          height: 40px;
          border-radius: 5px;
          border: 1px solid #e8d9c5;
        }
      }
      
      .action-btn {
        margin-top: 30px;
        
        .register-btn {
          background: linear-gradient(to right, #7C191E, #9e2a30);
          color: #fff;
          height: 50px;
          line-height: 50px;
          border-radius: 25px;
          font-size: 18px;
          font-weight: bold;
          box-shadow: 0 4px 10px rgba(124, 25, 30, 0.3);
          border: none;
          
          &:active {
            opacity: 0.9;
            transform: scale(0.98);
          }
        }
      }
    }
    
    .bottom-links {
      margin: 20px 0;
      text-align: center;
      
      .login-link {
        color: black;
        font-size: 14px;
        text-decoration: underline;
        opacity: 0.8;
      }
    }
  }
</style>